﻿namespace EcmaDoc.Core.Configuration
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Xml;

	/// <summary>
	/// Contains the configured external types.
	/// </summary>
	internal class ExternalTypeConfiguration
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ExternalTypeConfiguration"/> class, using the specified 
		/// <paramref name="configElement"/>.
		/// </summary>
		/// <param name="configElement">The configuration XML element that contains the external type information.</param>
		public ExternalTypeConfiguration(XmlNode configElement)
		{
			this.Types = new List<ExternalTypeInfo>();
			if (configElement != null)
				foreach (XmlElement typeElement in configElement.SelectNodes("c:type", ParserConfig.NamespaceManager))
					this.Types.Add(new ExternalTypeInfo(typeElement));
		}

		/// <summary>
		/// Gets the list of external types that have been configured.
		/// </summary>
		public List<ExternalTypeInfo> Types
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets one or more links configured for external type with the specified <paramref name="typeName"/>.
		/// </summary>
		/// <param name="typeName">The name of the type for which to return the URL(s).</param>
		/// <returns>One or more links configured for external type with the specified <paramref name="typeName"/>.</returns>
		public Dictionary<string, string> GetTypeUrl(string typeName)
		{
			Dictionary<string, string> result = new Dictionary<string, string>();

			var matchingType = this.Types.Where(t => t.Name == typeName || t.Aliases.Contains(typeName)).FirstOrDefault();
			if (matchingType != null)
				matchingType.Links.ForEach(l => 
					result.Add(l.Name, l.Url));

			return result;
		}
	}
}
